﻿package com.dg.gaming.api.common.util
{
	import com.ericfeminella.collections.HashMap;

	import mx.resources.IResourceManager;
	import mx.resources.ResourceBundle;
	import mx.resources.ResourceManager;

	/**
	 * ...
	 * @author
	 */
	public class AbstractProperties
	{
		public var filename:String;

		/** Resource bundle. */
		protected var rb:IResourceManager=null; // resource bundle

		/*
		   static {
		   URL url = null;
		   try {
		   url = new File(System.getProperty("user.dir")).toURI().toURL();
		   } catch (Exception ex) {
		   }
		   cl = new URLClassLoader(new URL[] {url},
		   AbstractProperties.class.getClassLoader());

		 }*/

		/**
		 * Private constructor (Can only be called by the getInstance() method.
		 * @param filename
		 */
		public function AbstractProperties(filename:String)
		{

			// ((URLClassLoader)(this.getClass().getClassLoader()))
			this.filename=filename;

			// Load resource bundles
			reload();
		}

		/**
		 * Reload all resource bundles (if there has been a
		 */
		public function reload():void
		{
			// Populate resource bundle
			try
			{
				//ResourceBundle.l
				rb=ResourceManager.getInstance();
			}
			catch (mrEx:Error)
			{
			}
		}

		/**
		 * Return the "value" for a specified "key" inside the resource bundle as a
		 * String.  If the value doesn't
		 *
		 * @param key           Key to get value from.
		 * @param defaultValue  Default value if key doesn't retrieve a value.
		 * @return              String value.
		 */

		public function getValue(key:String, defaultValue:String=null):String
		{
			if (rb != null)
			{
				return rb.getString(filename, key);
			}
			return defaultValue;
		}

//		/**
//		 * Return an array list of Keys;
//		 *
//		 * @return
//		 */
//		public function getKeys():ArrayList {
//			
//			var keys:ArrayList=ArrayList.createArrayList(rb.getKeys());
//			return keys;
//		}

		/**
		 * Return the "value" for a specified "key" inside the resource bundle as an
		 * int value.  If the value doesn't exist then return its defaultValue
		 * instead.
		 *
		 * @param key               Key to get value from.
		 * @param defaultValue      Default integer value if key isn't found.
		 * @return                  Integer value.
		 */
		public function getInt(key:String, defaultValue:int=-1):int
		{

			try
			{
				return parseInt(getValue(key));
			}
			catch (mrEx:Exception)
			{
				// Return the default value.
			}

			return defaultValue;
		}



		/**
		 * Return a boolean value from a key.  If the key doesn't exist then return
		 * the default value.
		 *
		 * @param key           Key to get value from.
		 * @param defaultValue  Default boolean value if key isn't found.
		 * @return              Returns true if value="true" or default value if not value exists.
		 */
		public function getBoolean(key:String, defaultValue:Boolean=false):Boolean
		{
			try
			{
				return getValue(key) == "true";
			}
			catch (mrEx:Exception)
			{
				// Return the default value.
			}

			return defaultValue;
		}

		/**
		 * Recreates ResourceBundle
		 */
		public function reset():void
		{
			reload();
		}

		public function getResourceBundle():IResourceManager
		{
			return rb;
		}

		public function listkeys():Array
		{

			var obj:Object=rb.getResourceBundle("en_US", filename).content;

			var keys:Array=[];
			for (var k:Object in obj)
			{
				keys.push(k);
			}
			return keys;
		}

		public function listItems():HashMap
		{
			var map:HashMap=new HashMap();
			var keys:Array=rb.getBundleNamesForLocale(filename);
			var len:int=keys.length;
			for (var i:int=0; i < 3; i++)
			{
				var key:String=keys[i];
				map.put(key, getValue(key));
			}
			return map;
		}
	}

}